﻿using System.Xml;
using System.Xml.Linq;
using Xunit;
using Xunit.Should;

namespace Adjunct.Framework.Nuspec
{
	public class NuspecDependencySpec
	{
		private const string ValidAllXml = @"<dependency id='xunit' version='1.8' />";
		private const string ValidMinXml = @"<dependency id='xunit' />";
		private readonly NuspecDependency _validAll = new NuspecDependency("xunit", "1.8");
		private readonly NuspecDependency _validMin = new NuspecDependency("xunit");

		[Fact]
		public void FromNullXElement()
		{
			XElement element = null;
			((NuspecDependency)element).ShouldBeNull();
		}

		[Fact]
		public void FromValidAllXElement()
		{
			((NuspecDependency)XElement.Parse(ValidAllXml)).ShouldEqual(_validAll);
		}

		[Fact]
		public void FromValidMinXElement()
		{
			((NuspecDependency)XElement.Parse(ValidMinXml)).ShouldEqual(_validMin);
		}

		[Fact]
		public void MissingIdToXElementThrows()
		{
			NuspecDependency dependency = new NuspecDependency();
			XmlException exception = Assert.Throws<XmlException>(() => (XElement)dependency);
			exception.Message.ShouldEqual("Can not create attribute 'id', the value was null.");
		}

		[Fact]
		public void NullToXElement()
		{
			NuspecDependency dependency = null;
			((XElement)dependency).ShouldBeNull();
		}

		[Fact]
		public void ValidAllToXElement()
		{
			((XElement)_validAll).ShouldEqual(ValidAllXml);
		}

		[Fact]
		public void ValidMinToXElement()
		{
			((XElement)_validMin).ShouldEqual(ValidMinXml);
		}
	}
}